package com.five.admin.interceptors;

import com.five.util.MySessionContext;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author keibdye
 */
public class AdminLoginInterceptors implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 设置编码格式
        request.setCharacterEncoding("UTF-8");

        if ( null ==  request.getSession(false)){
            System.out.println("用户未登录，重定向登录页");
            return false;
        }

        if (null == request.getRequestedSessionId()){
            System.out.println("用户未登录，重定向登录页");
            return false;
        }

        // 获取session对象
        HttpSession session = request.getSession();
        // 创建自定义 保存session的容器
        MySessionContext instance = MySessionContext.getInstance();
        // 保存session
        instance.addSession(session);
        return true;
    }





}
